
BIN=$(dirname "$0")
JOB_WORKDIR=$(cd "$BIN/.."; pwd)
SLEEP_TIME=10
DATE_FORMAT='+%Y-%m-%d %H:%M:%S'

# Usage:
#
# run_wf_and_wait <config-file> <workflow-name>
#
run_wf_and_wait() {
        config="$1"
	workflow="$2"
	echo "run_wf_and_wait(${config} ${workflow})"

	job_id_file="$JOB_WORKDIR/$workflow/last_oozie_job_id"
        echo "run_wf_and_wait() job_id_file=${job_id_file}"
        oozie job -run -config "$config" > $job_id_file

	job_id=$(cat $job_id_file | sed '{s/^job: //}')
	curr_time=$(date -d now "$DATE_FORMAT")
	echo "$curr_time $workflow started a new job, id: $job_id"
	sleep $SLEEP_TIME
	status=$(oozie job -info $job_id | grep "^Status" | sed '{s/^.*: //}')
	while [ "$status" == "RUNNING" ]; do 
		curr_time=$(date -d now "$DATE_FORMAT")
		echo "$curr_time $workflow job: $job_id  $status"
		sleep $SLEEP_TIME
		status=$(oozie job -info $job_id | grep "^Status" | sed '{s/^.*: //}')
	done
	curr_time=$(date -d now "$DATE_FORMAT")
	echo "$curr_time $workflow job: $job_id  $status"
	rm -f $job_id_file
}

